package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;


@Service
public class SetmealServiceImpl implements SetmealService {

    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @param setmealDTO
     * @return
     */
    @Override
    public PageResult pageList(SetmealPageQueryDTO setmealDTO) {
        PageHelper.startPage(setmealDTO.getPage(), setmealDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageSelect(setmealDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void InsetSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);


        //添加到setmeal表里
        setmealMapper.insertSetmeal(setmeal);

       //添加到setmealDish表里
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();

        setmealDishes.forEach(new Consumer<SetmealDish>() {
            @Override
            public void accept(SetmealDish setmealDish) {
                setmealDish.setSetmealId(id);
                //添加到setmealDish表里
                setmealDishMapper.insertSetmealDishes(setmealDish);
            }
        });
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmeal(Long id) {
        SetmealVO setmealVO=setmealMapper.selectById(id);

        //把setmealDishes拿出来
        Long id1 = setmealVO.getId();

        List<SetmealDish> setmealDishList = setmealDishMapper.selectSetmealDish(Collections.singletonList(id1));

        setmealVO.setSetmealDishes(setmealDishList);
        //把category的name拿出来
        Long categoryId = setmealVO.getCategoryId();
        Category category = categoryMapper.getById(categoryId);
        setmealVO.setCategoryName(category.getName());

        return setmealVO;
    }


    /**
     * 修改菜品信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改setmeal表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //先把所有关于该套餐id的信息删除
        setmealDishMapper.deleteById(setmealDTO.getId());
        //然后再添加新的套餐以及对应的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(new Consumer<SetmealDish>(){
            @Override
            public void accept(SetmealDish setmealDish) {
                setmealDish.setSetmealId(setmealDTO.getId());
                setmealDishMapper.insertSetmealDishes(setmealDish);
            }
        });
    }

    /**
     * 启动或者禁用
     * @param status
     * @param id
     */
    @Override
    public void StartOrStop(Integer status, Integer id) {
        setmealMapper.updateStatus(status,id);
    }

    /**
     * ids：删除对应id
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        //先判断是否能删除
       if (!ids.isEmpty()){

           ids.forEach(new Consumer<Long>() {
               @Override
               public void accept(Long ids) {
                   SetmealVO setmealVO = setmealMapper.selectById(ids);
                   if (setmealVO.getStatus()==1){//起售状态不删除
                       throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
                   }
               }
           });

           //删除setmeal表里的
           setmealMapper.deleteIds(ids);
           //删除setmeal_dish表
           ids.forEach(new Consumer<Long>() {
               @Override
               public void accept(Long id) {
                   setmealDishMapper.deleteById(id);
               }
           });
       }
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        return setmealMapper.getDishItemBySetmealId(id);
    }

}
